package com.wangshang.orgmgm.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wangshang.orgmgm.po.Workers;

/**
 * A data access object (DAO) providing persistence and search support for
 * Workers entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wangshang.orgmgm.po.Workers
 * @author MyEclipse Persistence Tools
 */

public class WorkersDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(WorkersDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String DEPARTMENT = "department";
	public static final String SALARY = "salary";
	public static final String ID_CARD_NUM = "idCardNum";
	public static final String TEL_NUM = "telNum";
	public static final String APPOINTMENT = "appointment";
	public static final String PLACE = "place";
	public static final String PASSWORD = "password";
	public static final String PLACEID = "placeid";
	public static final String HALL_ID = "hallId";
	public static final String DEP_TEL = "depTel";
	public static final String APPOINTMENT_ID = "appointmentId";

	protected void initDao() {
		// do nothing
	}

	public void save(Workers transientInstance) {
		log.debug("saving Workers instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Workers persistentInstance) {
		log.debug("deleting Workers instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Workers findById(java.lang.Integer id) {
		log.debug("getting Workers instance with id: " + id);
		try {
			Workers instance = (Workers) getHibernateTemplate().get(
					"com.wangshang.orgmgm.Workers", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Workers instance) {
		log.debug("finding Workers instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Workers instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Workers as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByDepartment(Object department) {
		return findByProperty(DEPARTMENT, department);
	}

	public List findBySalary(Object salary) {
		return findByProperty(SALARY, salary);
	}

	public List findByIdCardNum(Object idCardNum) {
		return findByProperty(ID_CARD_NUM, idCardNum);
	}

	public List findByTelNum(Object telNum) {
		return findByProperty(TEL_NUM, telNum);
	}

	public List findByAppointment(Object appointment) {
		return findByProperty(APPOINTMENT, appointment);
	}

	public List findByPlace(Object place) {
		return findByProperty(PLACE, place);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByPlaceid(Object placeid) {
		return findByProperty(PLACEID, placeid);
	}

	public List findByHallId(Object hallId) {
		return findByProperty(HALL_ID, hallId);
	}

	public List findByDepTel(Object depTel) {
		return findByProperty(DEP_TEL, depTel);
	}

	public List findByAppointmentId(Object appointmentId) {
		return findByProperty(APPOINTMENT_ID, appointmentId);
	}

	public List findAll() {
		log.debug("finding all Workers instances");
		try {
			String queryString = "from Workers";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Workers merge(Workers detachedInstance) {
		log.debug("merging Workers instance");
		try {
			Workers result = (Workers) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Workers instance) {
		log.debug("attaching dirty Workers instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Workers instance) {
		log.debug("attaching clean Workers instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static WorkersDAO getFromApplicationContext(ApplicationContext ctx) {
		return (WorkersDAO) ctx.getBean("WorkersDAO");
	}
}